/*
 * Copyright (C) 2014 VenomVendor <info@VenomVendor.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.bocai.dahuatech.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bocai.dahuatech.Bean.MyCollectionListBean;
import com.bocai.dahuatech.R;
import com.bumptech.glide.Glide;

import java.util.List;

public class MyCollectionAdapterHolder extends RecyclerView.Adapter<MyCollectionAdapterHolder.ViewHolder> {


    OnItemClickListener mItemClickListener;
    OnItemLongClickListener onItemLongClickListener;
    private Context context;
    List<MyCollectionListBean.ContentEntity> datas;
    public MyCollectionAdapterHolder(Context context,List<MyCollectionListBean.ContentEntity> datas) {
        this.context = context;
        this.datas=datas;
    }

    public void addDatas(List<MyCollectionListBean.ContentEntity> beans){
        datas.addAll(beans);
        this.notifyDataSetChanged();
    }



    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        final LayoutInflater mInflater = LayoutInflater.from(parent.getContext());
        final View sView = mInflater.inflate(R.layout.solution_list_item, parent, false);
        return new ViewHolder(sView);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        if(datas.get(position).getContent()!=null){

            holder.tvContent.setText(datas.get(position).getContent());
        }
        if(datas.get(position).getTitle()!=null) {
            holder.tvTitle.setText(datas.get(position).getTitle());
        }
        if(datas.get(position).getImageUrl()!=null) {
            Glide.with(context).load(datas.get(position).getImageUrl())
//                    .error(R.mipmap.product_photo_defalt)
//                    .placeholder(R.mipmap.product_photo_defalt)
                    .into(holder.ivImage);
        }
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    public void SetOnItemClickListener(final OnItemClickListener mItemClickListener) {
        this.mItemClickListener = mItemClickListener;
    }
    public void SetOnItemLongClickListener(final OnItemLongClickListener onItemLongClickListener) {
        this.onItemLongClickListener = onItemLongClickListener;
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }
    public interface OnItemLongClickListener {
        void onItemLongClick(View view, int position);
    }
    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener,View.OnLongClickListener {


        ImageView ivImage;
        TextView tvTitle;
        TextView tvContent;
        LinearLayout ll_itemview;
        public ViewHolder(View view) {
            super(view);
            ivImage= (ImageView) view.findViewById(R.id.iv_news_pic);
            tvTitle= (TextView) view.findViewById(R.id.tv_news_title);
            tvContent= (TextView) view.findViewById(R.id.tv_news_content);
            ll_itemview= (LinearLayout) view.findViewById(R.id.ll_itemview);
            view.setOnClickListener(this);
            view.setOnLongClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if (mItemClickListener != null) {
//                mItemClickListener.onItemClick(v, getPosition());
                mItemClickListener.onItemClick(v, getLayoutPosition());
            }
        }

        @Override
        public boolean onLongClick(View v) {
            if (onItemLongClickListener!=null)
            {
                onItemLongClickListener.onItemLongClick(v,getLayoutPosition());
            }
            return true;
        }
    }

}
